package com.coffee.sku.service.sku.impl;

import com.coffee.admin.api.common.service.RedisService;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.sku.api.sku.query.SkuQuery;
import com.coffee.sku.api.sku.result.SearchListResult;
import com.coffee.sku.api.sku.result.SkuResult;
import com.coffee.sku.api.sku.service.SkuService;
import com.coffee.sku.service.sku.bean.Sku;
import com.coffee.sku.service.sku.dao.SkuDao;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class SkuServiceImpl implements SkuService {
    @Resource
    private SkuDao skuDao;
    @DubboReference
    private RedisService redisService;

    @Override
    public Page<SkuResult> getPager(SkuQuery param, PagerInfo pr) {
        Page<SkuResult> pager = new Page<SkuResult>(pr.getStart(), pr.getPageSize());
        Integer count = skuDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<Sku> list = skuDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, SkuResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(SkuQuery param) {
        Integer count = skuDao.count(param);
        return count;
    }

    @Override
    public SkuResult getDetail(Integer id) {
        Sku item = skuDao.findById(id);
        if (item == null) {
            return null;
        }
        SkuResult result = new SkuResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<SkuResult> getList(SkuQuery param, PagerInfo pr) {
        List<Sku> list = skuDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, SkuResult.class);
    }

    @Override
    public List<SkuResult> getList(SkuQuery param) {
        List<Sku> list = skuDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, SkuResult.class);
    }

    @Override
    public List<SkuResult> getList() {
        SkuQuery param = new SkuQuery();
        List<Sku> list = skuDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, SkuResult.class);
    }


    @Override
    public void delete(Integer id) {
        skuDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(SkuResult result) throws BusinessException {
        Sku item = new Sku();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            skuDao.insert(item);
        } else {
            Sku tmp = skuDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            skuDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, SkuResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, SkuResult> map = new HashMap<Integer, SkuResult>();
        List<Sku> list = skuDao.findByIds(StringUtil.setToList(idSet));
        List<SkuResult> resultList = DTOUtils.convertList(list, SkuResult.class);
        for (SkuResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, SkuResult> getItemMap() {
        Map<Integer, SkuResult> map = new HashMap<Integer, SkuResult>();
        SkuQuery param = new SkuQuery();
        List<Sku> list = skuDao.page(param, 0, Integer.MAX_VALUE);
        List<SkuResult> resultList = DTOUtils.convertList(list, SkuResult.class);
        for (SkuResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Set<SearchListResult> getSearchList(Integer merchantId) {
        List<SearchListResult> result = new ArrayList<SearchListResult>();
        Object obj = redisService.get(merchantId + "search");
        if (obj != null) {
            try {
                if (obj instanceof ArrayList<?>) {
                    for (Object o : (List<?>) obj) {
                        result.add(SearchListResult.class.cast(o));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Set<SearchListResult> set = new HashSet<>(result);
        return set;
    }

    @Override
    public void addSearchList(Integer merchantId, String name) {
        List<SearchListResult> result = new ArrayList<SearchListResult>();
        Object obj = redisService.get(merchantId + "search");
        if (obj != null) {
            try {
                if (obj instanceof ArrayList<?>) {
                    for (Object o : (List<?>) obj) {
                        result.add(SearchListResult.class.cast(o));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        SearchListResult searchListResult = new SearchListResult();
        searchListResult.setName(name);
        result.add(0, searchListResult);
        redisService.set(merchantId + "search", result);
    }

    @Override
    public String deleteSearch(Integer merchantId) {
        redisService.del(merchantId + "search");
        return "SUCCESS";
    }


}
